'use strict';

const Service = require('egg').Service;
const sequelize = require('sequelize')
class MacService extends Service {
  async list(params) {
    let result = {
      count:"",
      rows:""
    }
    const where = {};
    where.oltId = params.OltId
    if(params.PhysicalPort){
      where.PhysicalPort = params.PhysicalPort
    }
    if(params.search){
      params.search = params.search.toLowerCase()
      where.mac = new RegExp(params.search)
    }
    result.count = await this.ctx.model.Mac.find(where).count('_id')
    result.rows = await this.ctx.model.Mac.find(where).skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    return result
  }
  async macPhysicalPort(params){
    let result = await this.ctx.model.Mac.collection.distinct('PhysicalPort',{oltId:params.OltId})
    // let result = await this.ctx.model.Mac.findAll({
    //   attributes: [[sequelize.literal('DISTINCT `PhysicalPort`'), 'PhysicalPort']],
    //   where:{oltId:params.OltId}
    // })
    let arr = [];
    arr.push('All');
    result.forEach(item=>{arr.push(item)});
    return arr;
  }
}

module.exports = MacService;
